package com.example.lottery.service.activitystatus.operater;

import com.example.lottery.dao.datobject.ActivityDO;
import com.example.lottery.dao.datobject.ActivityPrizeDO;
import com.example.lottery.dao.datobject.ActivityUserDO;
import com.example.lottery.dao.mapper.ActivityPrizeMapper;
import com.example.lottery.dao.mapper.ActivityUserMapper;
import com.example.lottery.service.dto.ConvertActivityStatusDTO;
import com.example.lottery.service.enums.ActivityPrizeStatusEnum;
import com.example.lottery.service.enums.ActivityUserStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Component
public class UserOperator extends AbstractActivityOperator{
    @Autowired
    private ActivityUserMapper activityUserMapper;
    @Override
    public Integer sequence() {
        return 1;
    }

    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId=convertActivityStatusDTO.getActivityId();
        List<Long> userIds=convertActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum targetStatus=convertActivityStatusDTO.getTargetUserStatus();
        if(activityId==null||targetStatus==null
                           || CollectionUtils.isEmpty(userIds)){
            return false;
        }
        List<ActivityUserDO> activityUserDOList=activityUserMapper.batchSelectByAUIDS(activityId,userIds);
        if(CollectionUtils.isEmpty(activityUserDOList)){
            return false;
        }
        //如果当前的活动状态与传入的活动状态一致 也不进行处理
       for(ActivityUserDO auDO:activityUserDOList){
           if(auDO.getStatus()
                   .equalsIgnoreCase(targetStatus.name())){
               return false;
           }
       }
        return true;

    }

    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId=convertActivityStatusDTO.getActivityId();
        List<Long> userIds=convertActivityStatusDTO.getUserIds();
        ActivityUserStatusEnum targetStatus=convertActivityStatusDTO.getTargetUserStatus();
        try{
            activityUserMapper.batchUpdateStatus(activityId,userIds,targetStatus.name());
            return true;
        }catch (Exception e){
            return false;
        }
    }
}
